package com.jit.monitor.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.jit.monitor.R;
import com.jit.monitor.model.response.MainResponse;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

/**
 * @author crazyZhangxl on 2018/12/21.
 * Describe:
 */
public class MainListShowAdapter extends RecyclerView.Adapter<MainListShowAdapter.ViewHolder>{
    private Context mContext;
    private List<MainResponse> mRecycleBeanList;

    public MainListShowAdapter(Context context) {
        mContext = context;
    }

    public void setNewData(List<MainResponse> mList){
        mRecycleBeanList = mList;
        notifyDataSetChanged();
    }

    @Override
    public MainListShowAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_main_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MainListShowAdapter.ViewHolder holder, int position) {
        List<MainResponse.ObjTypeOutListBean> objTypeOutList = mRecycleBeanList.get(position).getObjTypeOutList();
        if (objTypeOutList == null || objTypeOutList.size() == 0){

        }else {
            List<String> mValues = new ArrayList<>();
            for (int i=0;i<objTypeOutList.size();i++){
                mValues.add(objTypeOutList.get(i).getObjType()+" "+objTypeOutList.get(i).getTypeNum());
            }

            holder.mFlowLayout.setAdapter(new TagAdapter<String>(mValues) {
                @Override
                public View getView(FlowLayout parent, int position, String s) {
                    TextView tv = (TextView) LayoutInflater.from(mContext).inflate(R.layout.item_tv,
                            parent, false);
                    tv.setText(s);
                    return tv;
                }
            });

            holder.mFlowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
                @Override
                public boolean onTagClick(View view, int position, FlowLayout parent) {
                    Toast.makeText(mContext,"第几个"+position, Toast.LENGTH_SHORT).show();
                    return false;
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        if (mRecycleBeanList != null){
            return mRecycleBeanList.size();
        }
        return 0;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TagFlowLayout mFlowLayout;
        public ViewHolder(View itemView) {
            super(itemView);
            mFlowLayout = itemView.findViewById(R.id.tagFlowLayout);
        }
    }
}
